package com.javaweb.newfilter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;

@WebFilter(filterName = "FilterCharEncoding",
            urlPatterns = {"/*"},
          initParams = {@WebInitParam(name="encoding", value="UTF-8")}
)
public class FilterCharEncoding implements Filter {
    private String encoding ;
    @Override
    public void destroy() {
    }
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        /**
         * Normally, our filter logic was presented here, ie. before chain.dofilter() ;
         */
        req.setCharacterEncoding(this.encoding);
        System.out.println("[INFO] encodingFilter was finished to set the encoding.") ;
        chain.doFilter(req, resp);
        /*
        HERE, the request was finished to process.
         */
    }
    @Override
    public void init(FilterConfig config) throws ServletException {
        System.out.println("[INFO] encodingFilter was initialized.") ;
        this.encoding = config.getInitParameter("encoding") ;

    }

}
